<script lang="ts" setup>
import {useRoute} from 'vue-router'
import {ref} from "vue"
import {
  getByIdAdminRoutes,
  getByIdAdminUser,
  postAdminRoutes,
  postAdminUser,
  putAdminRoutes,
  putAdminUser
} from "../serve"
import JsImages from "./js-images.vue"
import {FileToBase64} from "../utils"
import {ElMessage} from "element-plus"
import JsEditor from "./js-editor.vue";

const
    route = useRoute(),
    fromData = ref({
      username: '',
      password: '',
      nickname: '',
      mail: '',
      avatar: '',
    }),
    loading = ref(false),
    isAdd = ref(false),
    fromRules = {
      username: [
        {
          required: true,
          min: 1,
          max: 15,
          message: '账号长度1-15位',
          trigger: 'trigger',
        },
      ],
      password: [
        {
          required: true,
          min: 1,
          max: 20,
          message: '密码长度1-20位',
          trigger: 'trigger',
        },
      ],
      nickname: [
        {
          required: true,
          min: 1,
          max: 20,
          message: '昵称长度1-20位',
          trigger: 'trigger',
        },
      ],
      mail: [
        {
          required: true,
          min: 1,
          max: 25,
          message: '请输入邮箱',
          trigger: 'trigger',
        },
      ],
      avatar: [
        {
          required: true,
          trigger: 'trigger',
          message: '请上传头像',
        },
      ]
    },
    froms = ref(),
    inImage = ref(false)
;

function get() {
  if (route.query['id']) {
    loading.value = true
    isAdd.value = false
    getByIdAdminUser(route.query['id']).then((r: any) => {
      fromData.value = r
      loading.value = false
      inImage.value = true
    }).catch(() => loading.value = false)
  } else {
    isAdd.value = true
  }
}

get()

function loadFile(e: any) {
  loading.value = true
  FileToBase64(e).then((r: any) => {
    fromData.value.avatar = r
    loading.value = false
  })
}

async function change() {
  try {
    await froms.value.validate()
    loading.value = true
    if (isAdd.value) {
      postAdminUser({...fromData.value}).then(() => {
        loading.value = false
        ElMessage({type: 'success', message: '新增成功！'})
      }).catch(() => loading.value = false)
      return
    }
    putAdminUser({...fromData.value}).then(() => {
      ElMessage({type: 'success', message: '修改成功！'})
      get()
    }).catch(() => loading.value = false)
  } catch (e) {
  }
}

function deleteChange() {
  fromData.value.avatar = ' '
}
</script>

<template>
  <el-scrollbar>
    <el-card v-loading="loading" box element-loading-text="加载中...">
      <template #header>{{ isAdd ? '用户添加' : '编辑信息' }}</template>
      <el-form ref="froms" :model="fromData" :rules="fromRules" el-From>
        <el-form-item label="账号：" prop="username">
          <el-input v-model="fromData['username']" maxlength="15" placeholder="请输入账号"/>
        </el-form-item>
        <el-form-item label="密码：" prop="password">
          <el-input v-model="fromData['password']" maxlength="20" show-password type="password" placeholder="请输入密码"/>
        </el-form-item>
        <el-form-item label="昵称：" prop="nickname">
          <el-input v-model="fromData['nickname']" maxlength="20" placeholder="请输入昵称" type="textarea" :rows="3"/>
        </el-form-item>
        <el-form-item label="邮箱：" prop="mail">
          <el-input v-model="fromData['mail']" maxlength="25" placeholder="请输入邮箱"/>
        </el-form-item>
        <el-form-item label="用户头像：" prop="avatar">
          <jsImages v-if="isAdd ? true : inImage" :picUrl="fromData.avatar" @myPicFile="loadFile"
                    @delete-change="deleteChange"/>
        </el-form-item>
        <el-form-item>
          <el-button round style="width: 80%;margin: auto;" type="success" @click="change">立即提交</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </el-scrollbar>
</template>

<style lang="scss" scoped>
[box] {
  width: 50%;
  margin: auto;
}

[el-From] {
  width: 98%;
  margin: auto;
}
</style>